﻿using Microsoft.Practices.Unity;
using UnityDemo.LoggingUtility;
using UnityDemo.Models;

namespace UnityDemo.DesignPatternSamples.Patterns
{
    public class SingletonSample : ISample
    {
        public SingletonSample()
        {
            ContainerManager();
            InstanceRegistration();
        }

        public void ContainerManager()
        {
            var container = new UnityContainer();

            container.RegisterType<IDuck, KingDuck>(new ContainerControlledLifetimeManager());

            var duck = container.Resolve<IDuck>();
            PrintHasCode(duck);

            var duck2 = container.Resolve<IDuck>();
            PrintHasCode(duck2);
        }

        public void InstanceRegistration()
        {
            var container = new UnityContainer();
            var duckInstance = new KingDuck();
            duckInstance.Name = "Hello King duck here";

            var duck = container.Resolve<IDuck>();
            PrintHasCode(duck);
        }

        private void PrintHasCode(IDuck duck)
        {
            LogerInstance.Logger.Write("Hash code of instance is " + duck.GetHashCode());
        }
    }
}